package coupon

import (
	sqlc "bp/db/sqlc"
	"bp/internal/render"
	// sharedConv "bp/internal/rpcserver/shared/conv"
	sharedVld "bp/internal/rpcserver/shared/vld"
	"bp/internal/service/entity/coupon"
	"bp/internal/util"
	couponGrpc "bp/rpc/gen/webapi/coupon"
	"context"
)

// VldGetCouponReq 验证获取优惠券请求
func (s *CouponWebApiImp) VldGetCouponReq(ctx context.Context, q *sqlc.Queries, req *couponGrpc.GetCouponReq) (couponID int64, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	if req.Id < 0 {
		err = render.InvalidArgumentRange("id")
		return
	}
	couponID = req.Id
	slog.Infof("coupon_imp/query_vld.VldGetCouponReq ok")
	return
}

// VldListCouponsReq 验证列出优惠券请求
func (s *CouponWebApiImp) VldListCouponsReq(ctx context.Context, q *sqlc.Queries, req *couponGrpc.ListCouponsReq) (ptr *coupon.ListCouponPtrsParam, err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	if !sharedVld.IsCouponStatusArray(req.CouponStatuses, true) {
		err = render.InvalidArgumentValue("coupon_statuses")
		return
	}
	if !sharedVld.IsOfferTypeArray(req.OfferTypes, true) {
		err = render.InvalidArgumentValue("offer_types")
		return
	}
	ptr = &coupon.ListCouponPtrsParam{
		CouponStatuses: req.CouponStatuses,
		OfferTypes:     req.OfferTypes,
		Keyword:        req.Keyword,
	}
	slog.Infof("coupon_imp/query_vld.VldGetCouponReq ok")
	return
}

// VldListCouponsReq 验证列出优惠券及其统计请求
func (s *CouponWebApiImp) VldListCouponWithStatsReq(ctx context.Context, q *sqlc.Queries, req *couponGrpc.ListCouponWithStatsReq) (ptr *coupon.ListCouponWithStatPtrsParam, err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	if !sharedVld.IsCouponStatusArray(req.CouponStatuses, true) {
		err = render.InvalidArgumentValue("coupon_statuses")
		return
	}
	if !sharedVld.IsOfferTypeArray(req.OfferTypes, true) {
		err = render.InvalidArgumentValue("offer_types")
		return
	}
	ptr = &coupon.ListCouponWithStatPtrsParam{
		CouponStatuses: req.CouponStatuses,
		OfferTypes:     req.OfferTypes,
		Keyword:        req.Keyword,
	}
	slog.Infof("coupon_imp/query_vld.VldListCouponWithStatsReq ok")
	return
}
